//
//  VRAddressBookCell.m
//  VRDoctor
//
//  Created by Veer on 16/5/27.
//
//

#import "VRAddressBookCell.h"

#import "VRAddressBookModel.h"


@interface VRAddressBookCell()

@property (nonatomic, weak) UIButton *checkBtn;
@property (nonatomic, weak) UILabel *nameLbl;
@property (nonatomic, weak) UILabel *phoneNumberLbl;
@property (nonatomic, weak) UIView *topLineView;
@property (nonatomic, weak) UIView *bottomLineView;

@end

@implementation VRAddressBookCell

- (void)setIsSelected:(BOOL)isSelected
{
    _isSelected = isSelected;
    self.checkBtn.selected = isSelected;
}

- (void)setLineType:(LineType)lineType
{
    _lineType = lineType;
    switch (lineType) {
        case LineTypeNone:
            self.topLineView.hidden = YES;
            self.bottomLineView.hidden = YES;
            break;
        case LineTypeTop:
            self.topLineView.hidden = NO;
            self.bottomLineView.hidden = YES;
            self.bottomLineView.transform = CGAffineTransformMakeTranslation(15, 0);
            break;
        case LineTypeCenter:
            self.topLineView.hidden = YES;
            self.bottomLineView.hidden = NO;
            self.bottomLineView.transform = CGAffineTransformMakeTranslation(15, 0);
            break;
        case LineTypeBottom:
            self.topLineView.hidden = YES;
            self.bottomLineView.hidden = NO;
            self.bottomLineView.transform = CGAffineTransformMakeTranslation(0, 0);
            break;
        case LineTypeTopAndBottom:
            self.topLineView.hidden = NO;
            self.bottomLineView.hidden = YES;
            self.bottomLineView.transform = CGAffineTransformMakeTranslation(0, 0);
            break;
        default:
            break;
    }
}


- (void)setModel:(VRAddressBookModel *)model
{
    _model = model;
    
    self.nameLbl.text = model.name;
    self.phoneNumberLbl.text = model.phoneNumber;
    
    self.checkBtn.selected = model.isSelected;
    //
}

+ (instancetype)cellWith:(UITableView *)tablevie
{
    static NSString *cellID = @"addMemberCell";
    
    VRAddressBookCell *cell = [tablevie dequeueReusableCellWithIdentifier:cellID];
    
    if (nil == cell) {
        cell = [[VRAddressBookCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setupSubView];
    }
    return self;
}


- (void)setupSubView
{
    // checkButton
    UIButton *checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [checkBtn setImage:[UIImage imageNamed:@"blank_icon"] forState:UIControlStateNormal];
    [checkBtn setImage:[UIImage imageNamed:@"checked_icon"] forState:UIControlStateSelected];
    [self.contentView addSubview:checkBtn];
    self.checkBtn = checkBtn;
    
    
    // 名字label
    UILabel *nameLbl = [[UILabel alloc] init];
    nameLbl.text = @"阿华";
    nameLbl.font = VRFont(16);
    [self.contentView addSubview:nameLbl];
    self.nameLbl = nameLbl;
    
    
    // 手机号label
    UILabel *phoneNumberLbl = [[UILabel alloc] init];
    phoneNumberLbl.text = @"18257149138";
    phoneNumberLbl.font = VRFont(15);
    phoneNumberLbl.textColor = VRColor(152, 151, 152);
    [self.contentView addSubview:phoneNumberLbl];
    self.phoneNumberLbl = phoneNumberLbl;
    
    // 顶部线
    UIView *topLineView = [[UIView alloc] init];
    topLineView.backgroundColor = VRColor(0xe0, 0xe0, 0xe0);
    [self.contentView addSubview:topLineView];
    self.topLineView = topLineView;
    topLineView.hidden = YES;
    
    // 底部线
    UIView *bottomLineView = [[UIView alloc] init];
    bottomLineView.backgroundColor = VRColor(0xe0, 0xe0, 0xe0);
    [self.contentView addSubview:bottomLineView];
    self.bottomLineView = bottomLineView;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@18);
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(@(15));
    }];
    
    [self.nameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(self.checkBtn.mas_right).offset(15);
    }];
    
    [self.phoneNumberLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-10);
        make.left.mas_equalTo(self.checkBtn.mas_right).offset(15);
    }];
    
    [self.topLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@0.5);
        make.width.equalTo(@(SCREEN_WIDTH));
        make.top.equalTo(self.contentView).offset(-0);
        make.left.equalTo(self.contentView);
    }];
    
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@0.5);
        make.width.equalTo(@(SCREEN_WIDTH));
        make.bottom.equalTo(self.contentView).offset(0);
        make.left.equalTo(self.contentView);
    }];
}

@end
